﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Ccr.Core;
using XcoAppSpaces.Core;

namespace XcoAppSpaces.Discovery
{
	class DiscoveryWorker : PDiscoveryWorker
	{
		private readonly IXcoDiscoveryService discoveryService;

		public DiscoveryWorker(IXcoDiscoveryService discoveryService)
		{
			this.discoveryService = discoveryService;
		}

		[XcoConcurrent]
		IEnumerator<ITask> ProcessDiscoveryRequest(DiscoveryRequest req)
		{
			foreach (var task in discoveryService.ProcessDiscoveryRequest(req))
			{
				yield return task;
			}
		}

		[XcoExclusive]
		void ProcessRegisterSpace(RegisterSpace msg)
		{
			discoveryService.Register(msg.SpaceAddress, msg.DiscoveryPort);
		}

		[XcoExclusive]
		void ProcessUnRegisterSpace(UnRegisterSpace msg)
		{
			discoveryService.UnRegister(msg.SpaceAddress);
		}
	}
}
